/*
* Image preview script 
* powered by jQuery (http://www.jquery.com)
* 
* written by Alen Grakalic (http://cssglobe.com)
* 
* for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
*
*/
var API = API || {};
API.imagePreview = function(){    
    /* CONFIG */

    xOffset = 10;
    yOffset = -30;
    doOffset = 50;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

    /* END CONFIG */
    $("a.preview").hover(function(e){
        this.t = this.title;
        this.title = "";    
        var c = (this.t != "") ? this.t : "";
        $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' /><span>"+ c +"</span></p>");                                 

        // Width of preview
        w = $('#preview').width();

        // Limit X to body borders
        x = ( (e.pageX + xOffset + w) < $("body").innerWidth() - 20 ) ? e.pageX + xOffset : $("body").innerWidth() - w-doOffset;

        $("#preview").css("top", (e.pageY - yOffset) + "px").css("left", (x) + "px").fadeIn("fast");                       
    },
    function(){
        this.title = this.t;    
        $("#preview").remove();
    });    
    $("a.preview").mousemove(function(e){

        // Width of preview
        w = $('#preview').width();

        // Limit X to body borders
        x = ( (e.pageX + xOffset + w) < $("body").innerWidth() - 20 ) ? e.pageX + xOffset : $("body").innerWidth() - w-doOffset;

        $("#preview").css("top", (e.pageY - yOffset) + "px").css("left", (x) + "px");
    });            
};

$(document).ready(function(){

    // Image Preview
    API.imagePreview();
        
    // Do focus on search term
    $('.search_term').live('click',function(){

        var input = $("#dest_hot")

        input.focus();
        tmpStr = input.val();
        input.val('');
        input.val(tmpStr);

    });
     
    var formbutton = '.home-subscribe .btn-primary';
    var submit_disabled = false;
    
    
    $(formbutton).live('click',function(){
        if(!submit_disabled)
            $('#subscrbe_form').submit();
    })
    
    //FORM VALIDATION
    $("#subscrbe_form").validate({
        rules: {
            subscribe_input: {
                required: true,
                email: true
            }
               
        }, 
        highlight: function(element) {
            $(element).addClass("error");
        },
        unhighlight: function(element) {
            $(element).removeClass("error");
        },
        submitHandler: function() {
            
            submit_disabled = true;
            
            $(formbutton).busy({
                img:base_url+'assets/img/backgnds/busy3.gif',
                position : 'left'
            });

            var my_form = $("#subscrbe_form");
            var form_data = my_form.serialize();
            var url = my_form.attr('action');
            
            $.ajax({
                url: url,
                data:form_data,
                dataType: 'json',
                success: function(data){
                    
                    $(formbutton).busy('hide');
                    
                    if(data.success){
                        alert('Thank you for subscribing.');
                    }else{
                        alert('Please try againg.');
                        submit_disabled = false;
                    }
                    
                    
                
                }
            });
               
            return false;
        },
        errorPlacement: function(error, element){}
    })
    
    $("#bookmarkme").click(function() {
//        console.log('click');
        if (window.sidebar) { // Mozilla Firefox Bookmark
                window.sidebar.addPanel(location.href,document.title,"");
            } else if( /*@cc_on!@*/false) { // IE Favorite
                window.external.AddFavorite(location.href,document.title); 
            } else if(window.opera && window.print) { // Opera Hotlist
                this.title=document.title;
                return true;
            } else { // webkit - safari/chrome
                alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
            }
    });

});
